package com.uy.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.uy.system.common.utils.SessionUtils;
import com.uy.system.model.dto.CaptchaResult;
import com.uy.system.model.dto.LoginDTO;
import com.uy.system.model.enums.HttpCode;
import com.uy.system.model.vo.LoginVo;
import com.uy.system.response.ResponseMessage;
import com.uy.system.response.ResponseMessageFactory;
import com.uy.system.service.IAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @Author LY
 * @create 2024/3/25 10:30
 * @Description 用户认证
 */
@Tag(name = "认证接口")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private IAuthService iAuthService;

    @Operation(summary = "登录")
    @PostMapping("/doLogin")
    public ResponseMessage<LoginVo> doLogin(@RequestBody LoginDTO dto) {
        LoginVo vo = iAuthService.doLogin(dto);
        return ResponseMessageFactory.getSuccessResponseMessage(vo);
    }

    @Operation(summary = "退出登录")
    @Parameters({
            @Parameter(name = "accessToken",description = "请求token",required = true,in = ParameterIn.HEADER)
    })
    @PostMapping("/logout")
    public ResponseMessage<String> logout(){
        String result = iAuthService.logout();
        SessionUtils.removeSysUser();
        if (StrUtil.isNotBlank(result))
            return ResponseMessageFactory.getErrorResponseMessage(HttpCode.INVALID_TOKEN.getCode(), result);
        else
            return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @Operation(summary = "获取验证码")
    @PostMapping("/captcha")
    public ResponseMessage<CaptchaResult> getCaptcha() {
        return ResponseMessageFactory.getSuccessResponseMessage(iAuthService.getCaptcha());
    }

}
